package com.way.service.impl;

import com.way.domain.dto.SysRoleDto;
import com.way.domain.entity.SysRole;
import com.way.repository.SysRoleRepository;
import com.way.service.SysRoleService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleRepository repository;

    @Override
    public List<SysRole> findAll() {
        return repository.findAll();
    }

    @Override
    public List<SysRole> findByName(String name) {
        return repository.findByName("%" + name + "%");
    }

    @Override
    public void save(SysRoleDto dto) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(dto, sysRole);
        sysRole.setFlag(1);
        sysRole.setIsDeleted(0);
        repository.save(sysRole);
    }

    @Override
    public void deleteById(Integer id) {
        repository.findById(id).ifPresent(sysRole -> {
            sysRole.setIsDeleted(1);
            repository.save(sysRole);
        });
    }

}
